/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// win_system.cpp - windows system stuff


#include "..\global.h"
#include <mmsystem.h>
#include "win_system.h"
#pragma comment (lib, "winmm.lib")


/*-----------------------------------------------------------------------------
	EXPORT STUFF
-----------------------------------------------------------------------------*/
DLL_EXPORT ISystem *CreateSystem(IConsole *con)
{
	return new EWinSystem(con);
}

DLL_EXPORT void DestroySystem(ISystem *sys)
{
	delete sys;
}


/*-----------------------------------------------------------------------------
	EWinSystem stuff :
-----------------------------------------------------------------------------*/

//
//	XTSystem::XTSystem
//
EWinSystem::EWinSystem(IConsole *con)
{
	this->con	=	con;
	
	timeBeginPeriod(1);
}


//
//	EWinSystem::~EWinSystem
//
EWinSystem::~EWinSystem(void)
{
	timeEndPeriod(1);
}



/*-----------------------------------------------------------------------------
	Program arguments
-----------------------------------------------------------------------------*/

const char *EWinSystem::GetCmdLine(void)
{
	return GetCommandLine();
}


/*-----------------------------------------------------------------------------
	Time
-----------------------------------------------------------------------------*/

uint EWinSystem::Milliseconds(void)
{
	return timeGetTime();
}


systemTime_t EWinSystem::GetGMTTime(void)
{
	systemTime_t	time;
	SYSTEMTIME	systime;
	
	::GetSystemTime(&systime);

	time.day			=	systime.wDay;	
	time.day_of_week	=	systime.wDayOfWeek;	
	time.hour			=	systime.wHour;	
	time.minute			=	systime.wMinute;	
	time.month			=	systime.wMonth;	
	time.msec			=	systime.wMilliseconds;	
	time.sec			=	systime.wSecond;	
	time.year			=	systime.wYear;	
	
	return time;
}


systemTime_t EWinSystem::GetLocalTime(void)
{
	systemTime_t	time;
	SYSTEMTIME	systime;
	
	::GetLocalTime(&systime);
	
	time.day			=	systime.wDay;	
	time.day_of_week	=	systime.wDayOfWeek;	
	time.hour			=	systime.wHour;	
	time.minute			=	systime.wMinute;	
	time.month			=	systime.wMonth;	
	time.msec			=	systime.wMilliseconds;	
	time.sec			=	systime.wSecond;	
	time.year			=	systime.wYear;	
	
	return time;
}



/*-----------------------------------------------------------------------------
	Process control
-----------------------------------------------------------------------------*/

void EWinSystem::Quit(uint code)
{
	PostQuitMessage(code);
}


void EWinSystem::Terminate(uint code)
{
	ExitProcess(code);
}



/*-----------------------------------------------------------------------------
	DLL Stuff :
-----------------------------------------------------------------------------*/

uint EWinSystem::DLL_Load(const char *dll_name)
{
	HMODULE	h_dll	=	LoadLibrary(dll_name);
	
	if (!h_dll) {
		RAISE_EXCEPTION(va("DLL \"%s\" is not loaded", dll_name));
	}
	
	return reinterpret_cast<uint>(h_dll);
}


void EWinSystem::DLL_Free(uint dll)
{
	BOOL	rval	=	FreeLibrary(reinterpret_cast<HMODULE>(dll));
	
	if (!rval) {
		RAISE_EXCEPTION("Wrong DLL descriptor");
	}
}


void* EWinSystem::DLL_GetProcAddr(uint dll, const char *func_name)
{
	void *proc	=	GetProcAddress(reinterpret_cast<HMODULE>(dll), func_name);
	
	if (!proc) {
		RAISE_EXCEPTION(va("Function \"%s\" is not found", func_name));
	}
	
	return proc;
}


/*-----------------------------------------------------------------------------
	Thread Stuff 
-----------------------------------------------------------------------------*/

thread_t	EWinSystem::SpawnThread(threadFunc_f func, void *param)
{
	HANDLE	h_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, param, 0, NULL);
	ASSERT(h_thread);
	
	return (thread_t)h_thread;
}

void EWinSystem::KillThread(thread_t thread)
{
	HANDLE	h_thread = (HANDLE)thread;
	VERIFY(TerminateThread(h_thread, 0));
	VERIFY(CloseHandle(h_thread));
}

void EWinSystem::Sleep(uint msec)
{
	::Sleep(msec);
}













